﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace Org.Eurekaa.DNJ.UI.DNJSettings
{

    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class UIEventFunction
    {
        private string _name = null;
        [Description("The name of function to be called for this event, this function can use event and ui arguments. Note that if you specify a function name here, the script (bellow option) will be ignored.")]
        [NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        string _body = "";
        [Description("Write the inline script you want to execute for this event, this script can access jquery event arguments : event and ui .\nNote that this script is ignored if you specify a function name to call (above option).")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(Org.Eurekaa.DNJ.UI.Design.Editors.UICodeEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string script
        {
            get { return _body; }
            set { _body = value; }
        }

        private List<string> _parameters;

        [Browsable(false)]
        [PersistenceMode(PersistenceMode.Attribute), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<string> Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        public UIEventFunction()
        {
            Parameters = new List<string>(new string[] { "event", "ui" });
        }

        public UIEventFunction(string[] parameters)
        {
            _parameters = new List<string>(parameters);
        }

        public UIEventFunction(string name)
        {
            this.Name = name;
            Parameters = new List<string>(new string[] { "event", "ui" });
        }

        public UIEventFunction(string name, string body)
        {
            this.Name = name;
            this._body = body;
            Parameters = new List<string>(new string[] { "event", "ui" });
        }

        [Browsable(false)]
        public bool External
        {
            get
            {
                Regex rxJsFName = new Regex("^[a-zA-Z_\\$]{1,}[a-zA-Z0-9_\\$]{1,}$");
                return rxJsFName.IsMatch(this._body);
            }
        }

        [Browsable(false)]
        public bool Empty
        {
            get
            {
                return (String.IsNullOrEmpty(_body) && String.IsNullOrEmpty(Name));
            }
        }
        public override string ToString()
        {
            if (Empty) return "";
            if (!String.IsNullOrEmpty(Name)) return Name;
            string args = "";
            foreach (string p in Parameters)
                args += (args == "") ? p : "," + p;

            return "function(" + args + "){" + _body + "}";


        }
    }

#region [ special event functions for DatePicker (hope this widget will be redesigned soon to avoid that :) ) ]
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class UIEventFnInput : UIEventFunction
    {
        public UIEventFnInput()
        {
            Parameters = new List<string>(new string[] { "input"});
        }
    }
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class UIEventFnDate : UIEventFunction
    {
        public UIEventFnDate()
        {
            Parameters = new List<string>(new string[] { "date"});
        }
    }
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class UIEventFnYMI : UIEventFunction
    {
        public UIEventFnYMI()
        {
            Parameters = new List<string>(new string[] { "year", "month", "inst" });
        }
    }
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class UIEventFnDtI : UIEventFunction
    {
        public UIEventFnDtI()
        {
            Parameters = new List<string>(new string[] { "dateText", "inst" });
        }
    }
#endregion

}
